/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.doeacc.tms.web.bean.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import edu.doeacc.tms.domain.User;
import edu.doeacc.tms.web.bean.UserBean;

/**
 *
 * @author slok
 */
@FacesConverter(forClass = User.class)
public class UserConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uic, String value) {
         if (value == null || value.length() == 0) {
                return null;
            }
            UserBean controller = (UserBean) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "userBean");
            return controller.getFacade().find(value);
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof User) {
            User o = (User) object;
            return o.getLoginId();
        } else {
            throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + User.class.getName());
        }
    }
}
